package com.marry.intelli.plugin;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.xml.XmlTag;
import com.marry.intelli.plugin.component.MJTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Map;

public class SearchDiaog extends JDialog {
    private static final String DEFAULT_PLACEHOLDER = "namespace.id";
    private JPanel contentPane;
    private JButton buttonOK;
    private MJTextField textField1;
    private JTextArea textArea1;

    private Map<String, XmlTag> sqlMap;

    private Project project;

    public void setProject(Project project) {
        this.project = project;
    }

    public void setSqlMap(Map<String, XmlTag> sqlMap) {
        this.sqlMap = sqlMap;
    }

    public JPanel getContainer() {
        return contentPane;
    }

    public SearchDiaog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        textField1.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (DEFAULT_PLACEHOLDER.equals(textField1.getText())) {

                    textField1.setText("");
                    textField1.setForeground(new Color(31, 41, 150));
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (textField1.getText().length() == 0) {
                    textField1.setText(DEFAULT_PLACEHOLDER);
                    textField1.setForeground(new Color(150, 150, 150));
                }

            }
        });



        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    public void setContextText(String text) {
        textArea1.append(text);

    }

    private void onOK() {
        String searchStr = textField1.getText();
        if ( searchStr == null || DEFAULT_PLACEHOLDER.equals(searchStr) || "".equals(searchStr.trim())) {
            Messages.showInfoMessage(project,"请输入查询条件", "提示");
            return;

        }

        if (!searchStr.contains(".")) {
            Messages.showInfoMessage(project,"查询条件格式错误，需要[namespace]+.+[sqlId]","提示");
            return;
        }
        XmlTag xmlTag = sqlMap.get(searchStr);
        if (xmlTag != null) {

            textArea1.setText(xmlTag.getText());
        } else {
            Messages.showInfoMessage(project,"未查询到相关信息","提示");
        }

    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        try {
            SearchDiaog dialog = new SearchDiaog();
            dialog.pack();
            dialog.setVisible(true);
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
